<?php
/**
 * This class use the Original MySQL API to operate the database, 
 * This extension is deprecated as of PHP 5.5.0, and will be removed in the future. 
 * Instead, the MySQLi or PDO_MySQL extension should be used.
 * But if you can not install them in your server, then you can use this.
 * if the php still support them.
 * 
 * 
 * @author Micheal Luthor
 * @version 0.0.0
 *
 */
X::import('Framework.XDatabase.Schema.XDbManager');

class MysqlDbManager extends XDbManager {
	/**
	 * @var string
	 */
	public $host;
	/**
	 * @var string
	 */
	public $user;
	/**
	 * @var string
	 */
	public $password;
	/**
	 * @var string
	 */
	public $database;
	
	/**
	 * @var resource
	 */
	public $conLink;
	
	/**
	 * Open a connection to a MySQL Server
	 * 
	 * @see XDbManager::connect()
	 * 
	 * @return MysqlDbManager
	 */
	public function connect($isPersistent=false){
		if ( $isPersistent )
			$this->conLink = mysql_pconnect($this->host, $this->user, $this->password);
		else
			$this->conLink = mysql_connect($this->host, $this->user, $this->password);
		
		if ( !$this->conLink ){
		// TODO : in the future, I think, there should be a throw thing. and we could show the stack by this.
			return false;
		}
		
		return $this;
	}
	
	/**
	 * @param string $databaseName
	 * @return MysqlDbManager
	 */
	public function select( $databaseName = '' ){
		mysql_query('USE '.$this->database);
		return $this;
	}

	/**
	 * @return MysqlDbManager
	 */
	public function close(){
		mysql_close($this->conLink);
		
		return $this;			
	}
	
	/**
	 * @param string $databaseName ]
	 * 
	 * @return MysqlDbManager
	 */
	public function create($databaseName){
		mysql_query('CREATE DATABASE '.$databaseName);
		return $this;
	}
	
	/**
	 * @param string $databaseName
	 * 
	 * @return MysqlDbManager
	 */
	public function drop($databaseName){
		mysql_query('DROP DATABASE '.$databaseName);
		
		return $this;
	}
	
	/**
	 * @return string
	 */
	public function clientEncoding(){
		return mysql_client_encoding($this->conLink);
	}
	
	/**
	 * @return array array('dbName1', 'dbName2')
	 */
	public function listDBNames(){
		$nameList = array();
		$dbList = mysql_list_dbs($this->conLink);
		
		$i = 0;
		$cnt = mysql_num_rows($dbList);
		while ($i < $cnt) {
			array_push($nameList, mysql_db_name($dbList, $i));
			$i++;
		}
		
		return $nameList;
	}
	
	/**
	 * @return string
	 */
	public function clientVersion(){
		return mysql_get_client_info();
	}
	
	/**
	 * @return string
	 */
	public function hostInfo(){
		return mysql_get_host_info($this->conLink);
	}
	
	/**
	 * @return string
	 */
	public function protoVersion(){
		return mysql_get_server_info($this->conLink);
	}
	
	/**
	 * @return string
	 */
	public function serverVersion(){
		return mysql_get_server_info();
	}
	
	/**
	 * @return array
	 */
	public function processes(){
		$processList = array();
		
		$result = mysql_list_processes($this->conLink);
		while (false !== ($row = mysql_fetch_assoc($result))){
			array_push($processList, $row);
		}
		mysql_free_result($result);
		
		return $processList;
	}
	
	/**
	 * @return bool
	 */
	public function ping(){
		return mysql_ping($this->conLink);
	}
	
	/**
	 * @param string $charSet
	 * @return MysqlDbManager
	 */
	public function setCharSet($charSet){
		mysql_set_charset($charSet, $this->conLink);
		return $this;
	}
	
	/**
	 * @return array
	 */
	public function stat(){
		$status = explode('  ', mysql_stat($this->conLink));
		return $status;
	}
	
	/**
	 * @return integer
	 */
	public function threadId(){
		return mysql_thread_id($this->conLink);
	}
	
}